package org.hzz.snake;

import static org.hzz.config.SnakeConstants.HEIGHT;
import static org.hzz.config.SnakeConstants.UNIT_SIZE;
import static org.hzz.config.SnakeConstants.WIDTH;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;

@Getter
@Setter
@Log
public class SnakeModel {
    
    private List<Point> snakeParts; // 贪吃蛇身体部分的坐标列表
    private Point food; // 食物的坐标
    private char direction; // 移动方向
    private boolean running; // 游戏是否正在运行

    public SnakeModel(){
        // 初始化
        snakeParts = new ArrayList<>();
        direction = 'R';
        running = false;
        spawnFood(); // 生成食物
    }

    public void newGame(){
        log.info("初始化游戏数值");
        snakeParts.clear();
        snakeParts.add(new Point(0,0));
        direction = 'R';
        running = true;
        spawnFood();
    }

    // 随机出现食物
    public void spawnFood() {
        log.info("随机出现食物");
        Random random = new Random();
        int x = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;
        int y = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;
        food = new Point(x,y);
    }

}

